﻿namespace ImageProcessing.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// zoom form
    /// </summary>
    public partial class ZoomForm : Form
    {
        /// <summary>
        /// image 1
        /// </summary>
        private DImage image1 = new DImage();

        /// <summary>
        /// Initializes a new instance of the <see cref="ZoomForm"/> class.
        /// </summary>
        public ZoomForm()
        {
            this.InitializeComponent();
            foreach (DImage img in Main.ImageList)
            {
                this.CmbImages.Items.Add(img.Name);
            }

            this.CmbImages.SelectedIndex = 0;

            this.image1.UpdateImage(Main.ImageList[this.CmbImages.SelectedIndex].ImageStructArray);
            this.picBox.Image = Operations.DisplayMatrix(this.image1.ImageStructArray);
            this.zoomSlider.Visible = false;
            this.MouseWheel += new MouseEventHandler(this.ZoomForm_MouseWheel);
        }

        /// <summary>
        /// Pictures the box zoom.
        /// </summary>
        /// <param name="img">The img.</param>
        /// <param name="size">The size.</param>
        /// <returns>image after zoom</returns>
        public Image PictureBoxZoom(Image img, Size size)
        {
            Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
            Graphics grap = Graphics.FromImage(bm);
            grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
            return bm;
        }

        /// <summary>
        /// Handles the MouseWheel event of the ZoomForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void ZoomForm_MouseWheel(object sender, MouseEventArgs e)
        {
                throw new NotImplementedException();
        }

        /// <summary>
        /// Handles the Load event of the ZoomForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ZoomForm_Load(object sender, EventArgs e)
        {
            // set Picture Box Attributes
            this.picBox.BackgroundImageLayout = ImageLayout.Stretch;
            this.panel1.AutoScroll = true;
            this.picBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

            // set Slider Attributes
            this.zoomSlider.Minimum = 1;
            this.zoomSlider.Maximum = 10;
            this.zoomSlider.SmallChange = 1;
            this.zoomSlider.LargeChange = 1;
            this.zoomSlider.UseWaitCursor = false;

            // reduce flickering
            this.DoubleBuffered = true;
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the CmbImages control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CmbImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.image1.UpdateImage(Main.ImageList[this.CmbImages.SelectedIndex].ImageStructArray);
            this.picBox.Image = Operations.DisplayMatrix(this.image1.ImageStructArray);
            this.picBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
        }

        /// <summary>
        /// Handles the Scroll event of the zoomSlider control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ZoomSlider_Scroll(object sender, EventArgs e)
        {
            Main.OperationName = "Picture Zoomed with" + this.zoomSlider.Value;
            if (this.zoomSlider.Value > 0)
            {
                this.picBox.Image = null;
                this.picBox.Image = this.PictureBoxZoom(this.image1.ImageBitmap, new Size(this.zoomSlider.Value, this.zoomSlider.Value));
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnOk_Click(object sender, EventArgs e)
        {
        }
    }
}
